<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>专辑页面</title>
		<script src="../../js/vue.js"></script>
		<script src="../../js/element.js"></script>
		<script src="../../js/axios.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../css/element.css" />
		<link rel="stylesheet" type="text/css" href="../../fonts/iconfont/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../../css/album/album.css"/>
	</head>
	<body>
		<div id="app">
			<!-- 头部专辑信息 -->
			<div class="header">
				<!-- 专辑图片 -->
				<span class="img">
					<img :src="album.albumImg">
				</span>
				<span class="albumInfo">
					<!-- 专辑名称 -->
					<div class="albumName">
						<h1>{{album.albumName}}</h1>
					</div>
					<!-- 歌手名 -->
					<div class="singerName"  >
						<span v-for="singer in album.singerList" @click="toSinger(singer.singerId)">{{singer.singerName}}</span>
					</div>
					<!-- 发行时间 -->
					<div class="releaseTime">
						{{album.albumReleaseTime}}
					</div>
					<!-- 歌单相关操作 -->
					<div class="albumButtons">
						<!-- 播放全部 -->
						<span class="button" @click="listPlayButton()">
							<i class="songListPlay iconfont icon-24gl-play"></i>
							播放全部
						</span>
						<!-- 收藏 -->
						<span class="button">
							<!-- 爱心 -->
							<span>
								<i v-if="album.isLove===0" class="iconfont icon-aixin" @click="changeLove(1, album.albumId)"></i>
								<i v-else class="iconfont icon-aixin_shixin" style="color: #FC6C62;" @click="changeLove(0, album.albumId)"></i>
							</span>
							收藏
						</span>
						<!-- 其他 -->
						<span class="otherButton">
							<i class="iconfont icon-gengduo"></i>
						</span>
					</div>
				</span>
			</div>
			<div class="main">
				<div class="main-header">
					<span class="nav1" @click="navChange(1)">歌曲</span>
					<span class="nav2" @click="navChange(2)">专辑信息</span>
					<span class="nav3" @click="navChange(3)">评论({{album.commentCount}})</span>
					<span id="hr"></span>
				</div>
				<div class="iframDiv">
					<iframe id="albumIframe" :src="navUrl" frameborder="0">
						
					</iframe>
				</div>
			</div>
		</div>
	</body>
	<script>
		
		new Vue({
			el: '#app',
			data: function() {
				return {
					url:'' ,//本页面的 url
					album:'' ,//专辑信息
					// 菜单
					navUrl: 'songList.html',//菜单指向的地址
					currentIndex: -1
				}
			},
			mounted() { // 生命周期函数
				this.getAlbumById()
				window.getChildrenSongCurrentIndex = this.getChildrenSongCurrentIndex
				window.listPlayButton = this.listPlayButton
				window.getIndexCurrentIndex = this.getIndexCurrentIndex
				window.toSinger = this.toSinger
				window.getAlbumById = this.getAlbumById
			},
			methods:{
				getChildrenSongCurrentIndex(currentIndex){
					this.currentIndex = currentIndex
				},
				// 菜单选中为其添加样式
				navChange(navId){
					console.log(navId)
					// 1、先去掉选择中的样式
					const navsDocument = document.querySelectorAll('.main-header span')
					for(var i=0; i< navsDocument.length;i++){
						navsDocument[i].style.color = '#000000'
					}
					this.navUrl = ''
					var albumId =  this.getUrlAlbumId()
					
					const hrDocument = document.querySelector('#hr')
					// 2、判断用户选中的是哪一个，加上样式
					if(navId == 1){
						const nav1Document = document.querySelector('.nav1')
						nav1Document.style.color = '#FC6C62'
						hrDocument.style.left = '20px'
						this.navUrl = 'songList.html?' + albumId
					}else if(navId == 2){
						const nav1Document = document.querySelector('.nav2')
						nav1Document.style.color = '#FC6C62'
						hrDocument.style.left = '165px'
						this.navUrl = 'info.html?' + albumId
					}else if(navId == 3){
						const nav1Document = document.querySelector('.nav3')
						nav1Document.style.color = '#FC6C62'
						hrDocument.style.left = '310px'
						this.navUrl = 'comment.html?' + albumId
					}
				},
				// 获取当前页面的 url 中的 albumId
				getUrlAlbumId(){
					// 1、获取当前页面的 url
					var url = window.location.href
					this.url = url
					console.log(url)
					// 2、字符串截取 获取 albumId
					var albumId = url.slice(url.indexOf('=')+1)
					console.log('专辑id', albumId)
					// 3、返回 albumId
					return '' + albumId
				},
				// 根据 albumId 查找专辑信息
				getAlbumById(){
					// 1、获取 albumId
					console.log(this.getUrlAlbumId())
					var albumId =  this.getUrlAlbumId()
					axios({
					    method: 'get',
					    url: 'http://localhost:8160/album/getOneById/' + albumId,
						headers: {
							'token': this.getCookie('yxy-pre:user_token')
						}
					}).then(res => {
						//处理返回的数据，如果登录成功，保留登录成功的状态，转向成功页面。如果登录不成功，提示用户
						if (res.data != undefined && res.data.code == 200) {
							console.log(res.data)
							this.album = res.data.data.album
							
							// 将数据发送给子页面
							// 这里需要暴露给全局，这样的话，子页面才能调用相对应的方法
							window.toChildData = this.album
						}else{//登陆失败
						    this.$message.error(res.data.message)
						}
					})	
				},
				// 播放全部
				listPlayButton(currentIndex){
					console.log('album歌曲下标' + currentIndex)
					this.getAlbumById()
					console.log(this.album.songList)
					// 1、将专辑的歌曲列表数据通过父页面方法传给父页面
					// 获取正在播放歌曲的下标，-1 表示还没有播放
					window.parent.getChildrenSongList(this.album.songList, currentIndex)
				},
				// 获取父页面正在播放的下标
				getIndexCurrentIndex(currentIndex){
					// 调用子页面方法将，正在播放歌曲下标传给子页面
					var iframeDocument =  document.getElementById("albumIframe")
					iframeDocument.contentWindow.getAlbumCurrentIndex(currentIndex)
				},
				// 通过值获取cookie
				getCookie(key) {
					// 如果浏览器中有多个cookie，会以 ；分隔 例如：a=a1;b=b1
					// 根据 ；将 cookie 字符串分割成数组 
					var cookieArr = document.cookie.split("; ");
					// 遍历数组
					for (var i = 0; i < cookieArr.length; i++) {
						// 再根据 = 分隔数组中的每一个元素，数组第一个就是key，第二个就是value
						var cookiePair = cookieArr[i].split("=");
						if (key == cookiePair[0]) {
							return decodeURIComponent(cookiePair[1]);
						}
					}
					return null;
				},
				// 跳转到歌手页面
				toSinger(id){
					console.log(id)
					const fatherHtml =  window.parent.document.querySelector("#indexIframe")
					fatherHtml.setAttribute('src', 'singer/singer.html?singerId='+id)
				},
				// 是否我喜欢
				changeLove(flag, id){
					axios({
						method: 'get',
						url: 'http://localhost:8160/user/changeLove/2/' + flag+'/'+ id,
						headers: {
							'token': this.getCookie('yxy-pre:user_token')
						}
					}).then(res => {
						//处理返回的数据，如果登录成功，保留登录成功的状态，转向成功页面。如果登录不成功，提示用户
						if (res.data != undefined && res.data.code == 200) {
							console.log(res.data)
							// window.parent.getSingerById()
							this.getAlbumById()
							// this.getFatherInfo()
						} else {
							this.$message.error(res.data.message)
						}
					})
				},
			},
		})
	</script>
</html>

